package com.tao.com.xiaowen.module.picture;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.tao.com.xiaowen.config.download.DownLoadUntils;
import com.tao.com.xiaowen.model.Model;
import com.tao.com.xiaowen.R;

import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * Created by Administrator on 2016/8/20.
 */
public class PictureContent extends AppCompatActivity {
    private ImageView picUrl;
    private PhotoViewAttacher photoViewAttacher;
    private ImageButton picDownBtn;
    private Boolean isDownLoad = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture_content);

        initView();
    }

    private void initView() {
        picUrl = (ImageView) findViewById(R.id.picture_url);
        picDownBtn = (ImageButton) findViewById(R.id.picture_download);

        final String pictureUrl = getIntent().getStringExtra("picUrl");

        Glide.with(this).load(pictureUrl)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .crossFade()
                .into(picUrl);

        photoViewAttacher = new PhotoViewAttacher(picUrl);
        picDownBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isDownLoad) {
                    DownLoadUntils.onDownLoadImage(getApplicationContext(), pictureUrl, handler);
                }else {
                    Toast.makeText(getApplicationContext(), "已经下载了", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Model.DOWNLOAD_SUCCESS:
                    Toast.makeText(getApplicationContext(), "下载成功", Toast.LENGTH_SHORT).show();
                    isDownLoad = true;
                    break;
                case Model.DOWNLOAD_FAILED:
                    Toast.makeText(getApplicationContext(), "下载失败", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
    };
}
